package com.icinfo.cloud.provider.punish.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.mapper.CaseHandlePersonRecordMapper;
import com.icinfo.cloud.provider.punish.service.ICaseHandlePersonRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseHandlePersonRecord;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseHandlePersonInfoDto;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * case_handle_person_record 对应的Service实现类.<br>
 * author: Kexin.Hu
 * date: 2024/3/18 15:25
 */
@Service
@Slf4j
public class CaseHandlePersonRecordServiceImpl extends MyBatisServiceSupport implements ICaseHandlePersonRecordService {

    @Resource
    private CaseHandlePersonRecordMapper caseHandlePersonRecordMapper;

    @Override
    public List<CaseHandlePersonInfoDto> getCaseHandlePersonRecordByCaseId(String caseId) {
        Example example = new Example(CaseHandlePersonRecord.class);
        example.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", Constant.STRING_1);
        List<CaseHandlePersonRecord> caseHandlePersonRecords = caseHandlePersonRecordMapper.selectByExample(example);
        if (CollUtil.isEmpty(caseHandlePersonRecords)) {
            throw new BusinessException("无办案人员信息！");
        }
        return caseHandlePersonRecords.stream().map(this::convertHandlePersonDto).collect(Collectors.toList());
    }

    /**
     * dto转换
     *
     * @param item
     * @return
     */
    private CaseHandlePersonInfoDto convertHandlePersonDto(CaseHandlePersonRecord item) {
        CaseHandlePersonInfoDto dto = new CaseHandlePersonInfoDto();
        dto.setHandlePersonId(item.getHandlePersonId());
        dto.setHandlePersonName(item.getHandlePersonName());
        dto.setHandlePersonTel(item.getHandlePersonTel());
        dto.setHandlePersonType(item.getHandlePersonType());
        return dto;
    }
}
